package com.cs.paoyer.administration.web.action;

import com.cs.paoyer.administration.business.model.AuthorizedByBranchVO;
import com.cs.paoyer.administration.business.model.ContractVO;
import com.cs.paoyer.administration.business.model.CostCenterByAuthorizedVO;
import com.cs.paoyer.administration.business.model.FDClassVO;
import com.cs.paoyer.administration.business.model.FDTypeVO;
import com.cs.paoyer.administration.business.model.WarehouseByBranchVO;
//import java.text.DateFormat;
//import java.text.SimpleDateFormat;
//import java.util.Calendar;
//import java.util.Date;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import java.util.Vector;
import java.util.Collection;

import com.cs.paoyer.administration.business.model.UserVO;
import com.cs.paoyer.administration.business.model.FDVO;
import com.cs.paoyer.administration.web.form.FDForm;
//import com.cs.paoyer.common.exception.CommonUserException;
//import com.cs.paoyer.administration.business.client.AdministrationDelegate;
import com.cs.paoyer.common.exception.UserException;
import com.cs.paoyer.common.exception.SystemException;
//import com.cs.paoyer.administration.util.AccessControl;
import com.cs.paoyer.administration.business.AdministrationMngr;
  
public class SearchFDAction extends Action  {
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    
    if(request.getSession().getAttribute("userInfo")==null)
    {
       response.sendRedirect("/crm/index.jsp");
    }
		
    FDForm fdForm = ( FDForm ) form;
    ActionMessages messages = new ActionMessages();
    //AdministrationDelegate delegate = new AdministrationDelegate();
    AdministrationMngr delegate = new AdministrationMngr();				
    HttpSession session = request.getSession();
    UserVO user = (UserVO) session.getAttribute("userInfo");
    FDVO fd = new FDVO();
    
    try
		{
        
        
        // Cargar informacion de sucursales
        AuthorizedByBranchVO branch = new   AuthorizedByBranchVO();
        branch.setAuthorizedInternalCode(user.getAuthorizedId());
				Collection branches = delegate.findAuthorizedBranchByCriterea(branch);
        
        if(branches!=null && branches.size()>0)
        {
               request.setAttribute("branches",branches);
        }
        
        // Cargar informacion de clases de fondos documentales
        
				Collection classes = delegate.findFDClassByCriterea(new FDClassVO());
        
        if(classes!=null && classes.size()>0)
        {
               request.setAttribute("classes",classes);
        }
        
        //validar si se selecciono una sucursal
        if(request.getParameter("warehouse")!=null)
        {
                AuthorizedByBranchVO branch1 = new   AuthorizedByBranchVO();
                branch1.setAuthorizedInternalCode(user.getAuthorizedId());
                branch1.setBranchInternalCode(fdForm.getBranchId());
                Vector branches1 = new Vector(delegate.findAuthorizedBranchByCriterea(branch1));
                request.setAttribute("selectedBranch",(AuthorizedByBranchVO)branches1.firstElement());
                fd.setBranchId(fdForm.getBranchId());
                request.setAttribute("actualRequest", fd);
                //Cargar informacion de contratos
                ContractVO contract = new   ContractVO();
                contract.setUserId(user.getAuthorizedId());
                Collection contracts = delegate.findContractByCriterea(contract);
                
                
                if(contracts!=null && contracts.size()>0)
                {
                       request.setAttribute("contracts",contracts);
                }
                //Cargar informacion de Bodegas
                WarehouseByBranchVO warehouse = new   WarehouseByBranchVO();
                warehouse.setBranchInternalCode(fd.getBranchId());
            
                Collection warehouses = delegate.findWarehouseBranchByCriterea(warehouse);
                
                if(warehouses!=null && warehouses.size()>0)
                {
                       request.setAttribute("warehouses",warehouses);
                }
                
                //Cargar informacion de centros de costo
                CostCenterByAuthorizedVO center = new  CostCenterByAuthorizedVO();
                center.setAuthorizedInternalCode(user.getAuthorizedId());
                Collection centers = delegate.findCostCenterAuthorizedByCriterea(center);
                
                if(centers!=null && centers.size()>0)
                {
                       request.setAttribute("centers",centers);
                }
                
        }
      
				if(request.getParameter("initial")!=null)
				{
            if(request.getParameter("initial").trim().equalsIgnoreCase("types"))
            {
                FDClassVO clas = new   FDClassVO();
                clas.setCode(fdForm.getFdClassId());
                Vector classes1 = new Vector(delegate.findFDClassByCriterea(clas));
                request.setAttribute("selectedClass",(FDClassVO)classes1.firstElement());
                
                //Cargar informacion de tipos de fondo documental
                FDTypeVO type = new  FDTypeVO();
                type.setFdClassCode(fdForm.getFdClassId());
                Collection types = delegate.findFDTypeByCriterea(type);
                
                if(types!=null && types.size()>0)
                {
                       request.setAttribute("types",types);
                }
            }        
            else
            {
                if(request.getParameter("consultType").trim().equalsIgnoreCase("1"))
                {
                    fd= new FDVO();
                    fd.setBranchId(fdForm.getBranchId());
                    fd.setContract(fdForm.getContract());
                    fd.setCostCenterId(fdForm.getCostCenterId());
                    fd.setWarehouseId(fdForm.getWarehouseId());
                    
                }
                else if(request.getParameter("consultType").trim().equalsIgnoreCase("2"))
                {
                    fd= new FDVO();
                    fd.setFd(fdForm.getFd());
                    fd.setFdType(fdForm.getFdType());
                    
                }
                else
                {
                    fd= new FDVO();                   
                    fd.setFdClassId(fdForm.getFdClassId());
                    fd.setFdTypeId(fdForm.getFdTypeId());
                    
                    
                }
                fd.setAuthorizedCode(user.getAuthorizedId());
                fd.setCutDate(fdForm.getCutDate());
                Collection FDList = delegate.findInventoryByCriterea(fd);
                session.removeAttribute("actualRequest");
            
                if(FDList!=null && FDList.size()>0)
                {
                    request.setAttribute("FDList",FDList);
                }
            }
				}
			
    }
		catch (UserException ue) 
		{
				messages.add("msgWarn",  new ActionMessage("loginForm.message."
																			+ ue.getExceptionVO().getId()));
				saveMessages(request, messages);
				return mapping.getInputForward();
      
    }
		catch ( SystemException se) 
		{
				messages.add("msgError",  new ActionMessage("loginForm.message."
																			+ se.getExceptionVO().getId()));
				saveMessages(request, messages);
				return mapping.getInputForward();
    }
    return mapping.findForward("success");
  }
}